package com.dingxiang.mobile.keyboard.demo;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;

import com.dingxiang.mobile.keyboard.api.DXKeyboardManager;
import com.dingxiang.mobile.keyboard.api.DXKeyboardOption;
import com.dingxiang.mobile.keyboard.api.DXKeyboardType;
import com.dingxiang.mobile.keyboard.api.IDXKeyboardListener;

/**
 * Created by blue on 2018/5/3.
 */

public class DXDialog extends Dialog {


    private Activity mActi;
    private static final String TAG = "DXKeyboardDialog";

    private View mView;

    public DXDialog(@NonNull Context context) {
        super(context, android.R.style.Theme_DeviceDefault_Dialog_NoActionBar);
        mActi = (Activity) context;

        setContentView(R.layout.activity_dialog);

        mView = findViewById(R.id.dialogView);

        DXKeyboardManager.getInstance().init(getContext());
        DXKeyboardManager.getInstance().setAdjustDialogView(mView);
        DXKeyboardManager.getInstance().setListener(new IDXKeyboardListener(){
            @Override
            public void onTextChange(CharSequence str) {
                Log.d(TAG, "onTextChange");
            }

            @Override
            public void onShow() {
                Log.d(TAG, "onShow");

            }

            @Override
            public void onHide() {
                Log.d(TAG, "onHide");
            }

            @Override
            public void onClickFinish() {
                Log.d(TAG, "onClickFinish");
                DXKeyboardManager.getInstance().hide();
            }

            @Override
            public void onKbHeightChange(int height) {

            }
        });

        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                Log.d(TAG, "onDismiss");
                DXKeyboardManager.getInstance().hide();
            }
        });


        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.width = params.height = WindowManager.LayoutParams.MATCH_PARENT;
        getWindow().setAttributes(params);

        findViewById(R.id.dialog_root).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });


    }




    @Override
    public void show() {
        super.show();

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
            	
                DXKeyboardManager.getInstance().clearContent();
                DXKeyboardOption option = new DXKeyboardOption();
                option.setFinishBtn(true, "登录");
                option.setKbType(DXKeyboardType.NORMAL);
                option.setRandomKey(true);
                DXKeyboardManager.getInstance().show(mActi, option);
            }
        }, 100);
        Log.d(TAG, "show");
    }

    @Override
    public void dismiss() {
        Log.d(TAG, "dismiss");
        super.dismiss();
    }


}
